package components.editor
{
	import UIFree.ObjectEvent;
	import UIFree.controls.popupMenu.uif_menuItem;
	import UIFree.controls.uif_gbutton;
	import UIFree.controls.uif_popupMenu;
	import UIFree.uif_component;
	
	import UIUtils.uif_util;
	
	import classes.c_events;
	
	import components.c_pcomponent;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class c_rect extends c_pcomponent {
		private var _popupMenu: c_elemMenu;
		public function c_rect(parent: c_dragRect) {
			super(parent, 'dragRect');
		}
		
		override protected function createComponents():void {
			super.createComponents();
			_popupMenu = new c_elemMenu(cl.popupMenuLayer,  {
				orientation	: uif_popupMenu.BOTTOMPOSITION,
				itemClass	: uif_menuItem,
				showEvent	: MouseEvent.ROLL_OVER
			}, 100, 100, 'popupmenu');

			_popupMenu.activeControl = dragRect;
			_popupMenu.addEvent(Event.SELECT, doSelectAction);
		}
		
		override protected function listenEvents():void {
			super.listenEvents();
			addEvent(c_events.CHANGECURRENTELEMENT, doChangeCurrentElement, workspace);
		}
		
		protected function get dragRect(): c_dragRect {
			return parent as c_dragRect;
		}
		
		private function doChangeCurrentElement(e: Object): void {
			_popupMenu.listMenu = dragRect.link?dragRect.link.actions:null;
			if (!_popupMenu.empty) _popupMenu.showMenu();
			else if (_popupMenu.visible) _popupMenu.setVisibility(false);
		}
		
		private function doSelectAction(e: ObjectEvent): void {
			if (e.object.event is Function) e.object.event();
			else workspace.dispatchEvent(new Event(e.object.event));
		}
		
	}
}